extends Node

enum s{
	idle=0,
	force_stopping=1,
	openning_box=2,
	closing_box=3,
	printing_text=4,
	waiting=5,
	jumping=6
}
var char_time=0.01
var sms=Tween.new()
var if_yield=true
var current_status=s.idle
var dlgbox=false
var dlghead=false
var font=DynamicFont.new()
signal accepted
signal jump#是否选择跳过
var jump#~
var jumpable=true
var wait_num_list=[]
var wait_list=[]
var wait_time_list=[0.1,0.2,0.5,1,2,4,5]
var pc=0
var tpc=0
var wtr=Timer.new()

func init():
	add_child(sms)
	add_child(wtr)
	dlgbox=$"/root/Node2D/tdlg/dlg"
	dlghead=$"/root/Node2D/tdlg/spking"
	font.font_data=load("res://data/fonts/凤凰点阵体16px.ttf")
	dlgbox.add_font_override("normal_font",font)
	dlgbox.update()

func open_box(if_ani=true):
	current_status=s.openning_box
	dlgbox.visible=true
	dlgbox.visible_characters=0
	dlghead.visible=true
	if(if_ani):
		sms.interpolate_property(dlgbox,"rect_size",Vector2(0,0),Vector2(940,240),1,Tween.TRANS_CUBIC,Tween.EASE_IN_OUT)
		sms.interpolate_property(dlgbox,"rect_position",Vector2(512,512),Vector2(78,264),1,Tween.TRANS_CUBIC,Tween.EASE_IN_OUT)
		sms.interpolate_property(dlghead,"position",Vector2(-32,296),Vector2(40,296),1,Tween.TRANS_CUBIC,Tween.EASE_IN_OUT)
		sms.start()
		if(if_yield):
			yield(sms,"tween_all_completed")
	else:
		dlgbox.rect_size=Vector2(940,240)
		dlgbox.rect_position=Vector2(78,264)
		dlghead.position=Vector2(40,296)
	current_status=s.idle

func close_box(if_ani=true):
	wtr.start(0)
	sms.playback_speed=1
	if(sms.is_connected("tween_step",self,"dstep")):
		sms.disconnect("tween_step",self,"dstep")
	if(if_ani):
		current_status=s.closing_box
		dlgbox.bbcode_text=""
		sms.interpolate_property(dlgbox,"rect_size",Vector2(940,240),Vector2(0,0),1,Tween.TRANS_CUBIC,Tween.EASE_OUT)
		sms.interpolate_property(dlgbox,"rect_position",Vector2(78,264),Vector2(512,512),1,Tween.TRANS_CUBIC,Tween.EASE_OUT)
		sms.interpolate_property(dlghead,"position",Vector2(40,296),Vector2(-32,296),1,Tween.TRANS_CUBIC,Tween.EASE_OUT)
		sms.start()
		if(if_yield):
			yield(sms,"tween_all_completed")
			
	else:
		dlgbox.rect_size=Vector2(0,0)
		dlgbox.rect_position=Vector2(512,512)
		dlghead.position=Vector2(-32,296)
	dlgbox.visible=true
	dlghead.visible=true
	current_status=s.idle

func resize_font(size,font_new=font):
	font=font_new
	font.size=size
	dlgbox.add_font_override("normal_font",font)
	dlgbox.update()

func h(texture):
	dlghead.texture=texture

func c():
	dlgbox.bbcode_text=""
	dlgbox.visible_characters=0

func p(text,character_time=char_time):
	current_status=s.printing_text
#	print("New dilogue started!")
	tpc=dlgbox.visible_characters-1-pc
	var t0=text
	var t1=0#richtext标签占字符数
	var t2=0#\n数
	wait_num_list=[]
	wait_list=[]
	#					「屎山核心」
	#初二留下的上古石山，因为不会正则表达式故手搓了一个字符串搜索&切除。
	var t6=0
	while(t6<t0.length()):#遍历对话字符串
		match(char(t0.ord_at(t6))):
			'\n':
				t2+=1
			'[':
				if(t6-1>0):
					if(char(t0.ord_at(t6-1))!='/'):
						while(char(t0.ord_at(t6))!=']'):
							t6+=1
							t1+=1
						t1+=1
					else:
						t0=t0.left(t6)+t0.right(t6+1)#剪掉[前面那个/
						t6-=1
				else:
					while(char(t0.ord_at(t6))!=']'):
						t6+=1
						t1+=1
					t1+=1
			'/':
				if(t6+3<=t0.length()):
					match(char(t0.ord_at(t6+1))):
						'/':
							t0=t0.left(t6+1)+t0.right(t6+2)#剪掉/后面那个/
							t6-=1
						'w':
							if((t0.ord_at(t6+2))>=48&&(t0.ord_at(t6+2)<=57)):
								wait_list.append(t6-1-t1-t2)
#								print("t1:",t1,"\nt2:",t2)
								var t7=t6+2
								while(t7<t0.length()):#48是0 57是9
									if((t0.ord_at(t7))>=48&&(t0.ord_at(t7)<=57)):
										t7+=1
									else:
										break
								var t9=0
								var t10=1
								for t8 in range(t7-1,t6+1,-1):
									t9+=(t0.ord_at(t8)-48)*t10
									t10*=10
								wait_num_list.append(t9)
								t0=t0.left(t6)+t0.right(t7)
								t6-=1
		t6+=1
#	print("wait_list:",wait_list,"\nwait_num_list:",wait_num_list)
	dlgbox.bbcode_text+=t0
	sms.interpolate_property(
		dlgbox,
		"visible_characters",
		dlgbox.visible_characters,
		dlgbox.text.length()-t2,
		(dlgbox.text.length()-t2)*character_time,
		Tween.TRANS_LINEAR
	)
	if(!sms.is_connected("tween_step",self,"dstep")):
		sms.connect("tween_step",self,"dstep")
	pc=dlgbox.visible_characters
	sms.start()
	if(if_yield):
		yield(sms,"tween_all_completed")
	current_status=s.idle

func dstep(_e0,_e1,_e2,_e3):
#	print(dlgbox.visible_characters-1-pc)
	while((wait_list.size()>0)&&(!jump)&&(current_status==s.printing_text)):
		if(tpc<wait_list[0]&&(dlgbox.visible_characters-1-pc)>wait_list[0]):
#			print("Fixed 1 jump! ","previous_char:",tpc," current_char:",dlgbox.visible_characters-1-pc," top_wait:",wait_list[0]," pc:",pc)
			dlgbox.visible_characters=wait_list[0]+1+pc
		if(wait_list[0]==dlgbox.visible_characters-1-pc):
#			print("wait ",wait_time_list[wait_num_list[0]]," seconds.")
			sms.playback_speed=0
			wtr.start(wait_time_list[wait_num_list[0]])
			yield(wtr,"timeout")
			sms.playback_speed=1
			wait_list.pop_front()
			wait_num_list.pop_front()
		else:
			break
	tpc=dlgbox.visible_characters-1-pc
#	else:
#		print("Jumped!")

func n():
	current_status=s.waiting
	yield(self,"accepted")
	jump=false
	dlgbox.bbcode_text=""
	dlgbox.visible_characters=0
	current_status=s.idle

func s():
	current_status=s.waiting
	yield(self,"accepted")
	current_status=s.idle

func _process(_delta):
	if(i.keys.accept):
		if(current_status==s.waiting):
			emit_signal("accepted")
		elif(current_status==s.printing_text&&jumpable):
			emit_signal("jump")
			wtr.start(0)
			jump=true
			sms.stop(dlgbox,"visible_characters")
			sms.remove(dlgbox,"visible_characters")
			sms.playback_speed=1
			var t3=dlgbox.text
			var t4=0
			for t5 in t3:
				if(t5=='\n'):
					t4+=1
			dlgbox.visible_characters=dlgbox.text.length()-t4
			if(if_yield):
				sms.emit_signal("tween_all_completed")
